/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.entity.Danhgia;
import model.entity.PhongTro;
import model.entity.Quan;
import model.service.DanhgiaDAOService;

/**
 *
 * @author duy
 */
public class DanhGiaDAO implements DanhgiaDAOService {

    private static DanhGiaDAO danhGiaDAO;

    public static DanhGiaDAO getInstance() {
        if (danhGiaDAO == null) {
            danhGiaDAO = new DanhGiaDAO();
        }
        return danhGiaDAO;
    }

    @Override
    public List<Danhgia> getAlldanhgia() {
        List<Danhgia> list = new ArrayList<>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tbl_danhgia";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Danhgia item = new Danhgia();
                item.setMadanhgia(rs.getInt("madanhgia"));
                item.setDiemdanhgia(rs.getFloat("diemdanhgia"));
                item.setSonguoidanhgia(rs.getInt("songuoidanhgia"));
                item.setDiachiMAC(rs.getString("diachiMAC"));
                list.add(item);
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;

    }

    @Override
    public Danhgia getDanhgiabyID(int madanhgia) {
        Danhgia item = new Danhgia();

        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tbl_danhgia where madanhgia=? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, madanhgia);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                item.setMadanhgia(rs.getInt("madanhgia"));
                item.setDiemdanhgia(rs.getFloat("diemdanhgia"));
                item.setSonguoidanhgia(rs.getInt("songuoidanhgia"));
                item.setDiachiMAC(rs.getString("diachiMAC"));
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }

        return item;

    }

    @Override
    public boolean update_DanhgiabyMaphong(int madanhgia, float diemdanhgia, int songuoidanhgia, String diachiMAC) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();//thieu dau phay cho nay
            String sql = "update tbl_danhgia set diemdanhgia=?,songuoidanhgia=?,diachiMAC=? where madanhgia = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);

            pstmt.setFloat(1, diemdanhgia);
            pstmt.setInt(2, songuoidanhgia);
            pstmt.setString(3, diachiMAC);
            pstmt.setInt(4, madanhgia);
            pstmt.executeUpdate();
            isCheck = true;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return isCheck;
    }

    @Override
    public boolean createDanhGia(Danhgia danhgia) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();//thieu dau phay cho nay
            String sql = "insert into tbl_danhgia(madanhgia,diemdanhgia, songuoidanhgia, diachiMAC) values (?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);

            pstmt.setInt(1, danhgia.getMadanhgia());
            pstmt.setFloat(2, danhgia.getDiemdanhgia());
            pstmt.setInt(3, danhgia.getSonguoidanhgia());
            pstmt.setString(4, danhgia.getDiachiMAC());
            pstmt.executeUpdate();
            isCheck = true;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return isCheck;
    }

    @Override
    public int getMAXmadanhgia() {
        try {
            Connection conn = ConnectionFactory.getConnection();//thieu dau phay cho nay
            String sql = "SELECT MAX(madanhgia) AS db_phongtro FROM tbl_danhgia";
            PreparedStatement pstmt = conn.prepareStatement(sql);

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return 3;
    }

    @Override
    public int getMax() {
        try {
            Connection conn = ConnectionFactory.getConnection();//thieu dau phay cho nay
            String sql = "SELECT MAX(madanhgia) AS db_phongtro FROM tbl_danhgia";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            if (rs != null) {
                if (rs.next()) {
                    return rs.getInt("db_phongtro");
                }
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
